package first.endtoend;

import java.io.File;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.cookie.Cookie;
import org.json.JSONObject;

import android.R.bool;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;

import org.mbds.wolf.seql.exceptions.APDUError;

import com.google.android.gcm.GCMRegistrar;
import com.google.gson.reflect.TypeToken;

import first.endtoend.facades.AddressFacade;
import first.endtoend.facades.AidFacade;
import first.endtoend.facades.BeneficiaryFacade;
import first.endtoend.facades.CategoryFacade;
import first.endtoend.facades.FamilyFacade;
import first.endtoend.facades.PortfolioDetailFacade;
import first.endtoend.facades.PortfolioFacade;
import first.endtoend.facades.ProductFacade;
import first.endtoend.facades.RationCardFacade;
import first.endtoend.facades.TraceDetailFacade;
import first.endtoend.facades.TraceFacade;
import first.endtoend.helpers.Constant;
import first.endtoend.helpers.DialogHelper;
import first.endtoend.helpers.InternetHelper;
import first.endtoend.helpers.JsonHelper;
import first.endtoend.models.Address;
import first.endtoend.models.Aid;
import first.endtoend.models.Beneficiary;
import first.endtoend.models.Category;
import first.endtoend.models.FIAgent;
import first.endtoend.models.Family;
import first.endtoend.models.Portfolio;
import first.endtoend.models.PortfolioDetail;
import first.endtoend.models.Product;
import first.endtoend.models.RationCard;
import first.endtoend.models.Trace;
import first.endtoend.models.TraceDetail;



public class LoginActivity extends MyActivity {
	String username, password, message, regId;
	public static FIAgent user;
	public static Trace trace;
	public static List<Cookie> cookies;
	public static String usernameStored, passwordStored;
	AlertDialog.Builder alert;
	SharedPreferences settings;
	String PREF_NAME = "my_pref_settings";
	ProgressDialog dialog;
	Boolean hasRun;
	EditText editLogin, editPassword;
	CategoryFacade categoryFacade;
	ProductFacade productFacade;
	AidFacade aidFacade;
	FamilyFacade familyFacade;
	BeneficiaryFacade beneficiaryFacade;
	PortfolioFacade portfolioFacade;
	PortfolioDetailFacade portfolioDetailFacade;
	AddressFacade addressFacade;
	RationCardFacade rationCardFacade;
	TraceFacade traceFacade;
	AQuery aquery;
	String server_path = null;
	String port_number = null;
	String server_module = null;

	// convert the port number entered by the user from number to string
	String server_port_nb = port_number; // String.valueOf(port_number);

	private static final String TAG = "LoginActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		getActionBar().hide();
		dialog = new ProgressDialog(this);
		aquery = new AQuery(this);
		settings = getSharedPreferences(PREF_NAME, 0); // load the preferences
		hasRun = settings.getBoolean("hasRun", false); // see if it's run
														// before, default no
		editLogin = (EditText) findViewById(R.id.editLogin);
		editPassword = (EditText) findViewById(R.id.editPassword);
		categoryFacade = new CategoryFacade(this);
		productFacade = new ProductFacade(this);
		aidFacade = new AidFacade(this);
		familyFacade = new FamilyFacade(this);
		beneficiaryFacade = new BeneficiaryFacade(this);
		portfolioFacade = new PortfolioFacade(this);
		portfolioDetailFacade = new PortfolioDetailFacade(this);
		addressFacade = new AddressFacade(this);
		rationCardFacade = new RationCardFacade(this);

		server_path = settings.getString("url", null);
		port_number = settings.getString("portNumb", null);
		server_module = settings.getString("module", null);
		if (server_path == null || port_number == null || server_module == null)
			config();

		Button connexion = (Button) findViewById(R.id.boutonConnexion);
		connexion.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				username = editLogin.getText().toString();
				password = editPassword.getText().toString();

				if ((username.equals("")) && (password.equals(""))) {
					// Toast.makeText(LoginActivity.this, R.string.fill,
					// Toast.LENGTH_SHORT).show();
					Toast.makeText(LoginActivity.this,
							"user name or passowrd are empty",
							Toast.LENGTH_SHORT).show();
				} else {

					if (!hasRun) {
						// first time the app run on the device
						Log.d(TAG, "first time this app run");
						loginOnServer(username, password);
					} else {
						Log.d(TAG, "this app has run before");
						ctrl.execute(
								"select username, password from pds_applet",
								Constant.GET_LOGIN_INFO_CODE);
					}
				}
			}
		});
	}

	void config() {
		Intent intent = new Intent(this, ConfigurationActivity.class);
		startActivity(intent);
		finish();
	}

	/**
	 * Method to process login, either online or offline
	 * 
	 * @param username
	 * @param password
	 */
	private void checkLoginInfo(String username, String password) {

		if (loginAndPasswordOk(username, password)) {
			user = retrieveUserStored();
			// Intent intent = new Intent(this, TagActivity.class);
			Intent intent = new Intent(this, IntermediateActivity.class);
			startActivity(intent);
			finish();
		} else {
			alert = new AlertDialog.Builder(this);
			DialogHelper.showErrorDialog(alert, getString(R.string.error),
					getString(R.string.wrong));
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == Constant.PIN_LAUNCHED_REQUEST
				&& resultCode == RESULT_OK) {
			if (loginAndPasswordOk(username, password)) {
				if (!hasRun) {
					loginOnServer(username, password);
				} else {
					user = retrieveUserStored();
					// Intent intent = new
					// Intent(LoginActivity.this,TagActivity.class);
					Intent intent = new Intent(LoginActivity.this,
							IntermediateActivity.class);
					startActivity(intent);
					finish();
				}
			} else {
				alert = new AlertDialog.Builder(LoginActivity.this);
				DialogHelper.showErrorDialog(alert, getString(R.string.error),
						getString(R.string.wrong));
			}

		}
	}

	/**
	 * method to connect to the backend's URL entered by the user in the
	 * configuration menu
	 * 
	 * @return
	 */
	public String getServerUrl() {
		String server_url = server_path + ":" + port_number + "/"
				+ server_module;
		if (!server_url.endsWith("/"))
			server_url += "/";
		return server_url;
	}

	private void loginOnServer(String username, String password) {
		// code: if this is the first time the app has run
		/*
		 * String url = getString(R.string.url)+ getString(R.string.connexion);
		 * if (InternetHelper.isOnline(LoginActivity.this)) { async_post(url,
		 * true, false, username, password); } else { alert = new
		 * AlertDialog.Builder(LoginActivity.this); showInternetErrorDialog(0);
		 * }
		 */

		String url = this.getServerUrl() + getString(R.string.connexion);
		if (InternetHelper.isOnline(LoginActivity.this)) {
			async_post(url, true, false, username, password);
		} else {
			alert = new AlertDialog.Builder(LoginActivity.this);
			showInternetErrorDialog(0);
		}

	}

	// @Override
	// public void onBackPressed() {
	// AlertDialog.Builder alert = new AlertDialog.Builder(this);
	// alert.setMessage(getString(R.string.quitconfirm));
	// alert.setPositiveButton(R.string.yes,
	// new DialogInterface.OnClickListener() {
	// @Override
	// public void onClick(DialogInterface dlg, int sumthin) {
	// finish();
	// }
	// });
	// alert.setNegativeButton(R.string.no,
	// new DialogInterface.OnClickListener() {
	// @Override
	// public void onClick(DialogInterface dialog, int which) {
	// dialog.dismiss();
	// }
	// });
	// alert.show();
	// }

	@Override
	public void onBackPressed() {

		alert = new AlertDialog.Builder(this);
		alert.setTitle(R.string.warning);
		// alert.setMessage(R.string.cancelToTag);
		alert.setMessage(getString(R.string.quitconfirm));
		alert.setPositiveButton(R.string.yes,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent(LoginActivity.this,
								ConfigurationActivity.class);
						startActivity(intent);
						finish();
					}
				});

		alert.setNegativeButton(R.string.no,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				});

		alert.show();
	}

	/**
	 * Method for connection online
	 * 
	 * @param url
	 * @param ajax
	 * @param c
	 * @param username
	 * @param password
	 */
	public void async_post(String url, boolean ajax, boolean c,
			final String username, final String password) {

		DialogHelper.showProcessingDialog(dialog, getString(R.string.login),
				getString(R.string.wait));

		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put(Constant.SPRING_SECURITY_USERNAME, username);
			params.put(Constant.SPRING_SECURITY_PASSWORD, password);
			params.put(Constant.SPRING_SECURITY_REMEMBER_ME, "true");
			AjaxCallback<String> cb = new AjaxCallback<String>() {
				@Override
				public void callback(String url, String json, AjaxStatus status) {
					switch ((status.getCode())) {
					case Constant.REQUEST_OK: // request http success
						// get the cookies set by spring security
						cookies = status.getCookies();
						int code = JsonHelper.getResponseCodeFromJson(json);
						switch (code) {
						case Constant.REQUEST_OK:

							cookies = status.getCookies();
							user = JsonHelper.getObjectFromJson(json,
									FIAgent.class, Constant.USER);
							SharedPreferences.Editor edit = settings.edit();
							edit.putString(Constant.USER, JsonHelper
									.createJsonObject(user).toString());

							usernameStored = user.getUsername();
							// Car, password cannot be read from server (because
							// of Spring security: it is encrypted and not
							// transmitted
							passwordStored = password;

							for (Cookie cookie : cookies) {
								if (cookie.getName()
										.equals(Constant.SESSION_ID)) {
									edit.putString("cookieName",
											cookie.getName());
									edit.putString("cookieValue",
											cookie.getValue());
									// edit.putLong("cookieExpiryDate",
									// cookie.getExpiryDate().getTime());
									edit.commit(); // apply
								}
							}
							storeUser();
							// set username and password into the applet at each
							// connection
							if (!hasRun) {
								ctrl.execute(
										"insert into pds_applet (username, password, key) values("
												+ user.getUsername() + ","
												+ password + ",1234)",
										Constant.SET_LOGIN_INFO_CODE);
							} else {
								ctrl.execute(
										"update pds_applet set (username, password) values("
												+ user.getUsername() + ","
												+ password + ")",
										Constant.SET_LOGIN_INFO_CODE);
							}
							load_categories();
							// checkLoginInfo(username, password);
							break;

						case Constant.NOT_FOUND:
							alert = new AlertDialog.Builder(LoginActivity.this);
							DialogHelper.showErrorDialog(alert,
									getString(R.string.error),
									getString(R.string.wrong));
							break;
						}
						break;

					default: // Request http failed

						String msg = getString(R.string.code_404) + " "
								+ getString(R.string.request_not_received);
						showErrorDialog(msg, 0);
						break;
					}
					cookies = status.getCookies();
				}
			};
			cb.header("X-Requested-With", "true");

			aquery.progress(dialog).ajax(url, params, String.class, cb);

		} catch (Exception e) {
			e.printStackTrace(); // To change body of catch statement use File |
		}
	}

	/**
	 * 
	 * @return the GCM Registration Id for this phone
	 */
	public String getGCMRegistrationId(Context context) {
		GCMRegistrar.checkDevice(context);
		GCMRegistrar.checkManifest(context);
		regId = GCMRegistrar.getRegistrationId(context);
		if (regId == "") {
			GCMRegistrar.register(context, Constant.GCM_ID);
			regId = GCMRegistrar.getRegistrationId(context);
		}
		return regId;
	}

	/**
	 * 1st web service called to send the GCM RegId while connect with the
	 * backend
	 * */
	public void sendRegId() {
		if (InternetHelper.isOnline(LoginActivity.this)) {
			final String cookieName = settings.getString("cookieName", "");
			final String cookieValue = settings.getString("cookieValue", "");

			String regId = getGCMRegistrationId(getApplicationContext());
			// String url = getString(R.string.url)+
			// getString(R.string.sendRegId);
			String url = this.getServerUrl() + getString(R.string.sendRegId);
			DialogHelper.showProcessingDialog(dialog,
					getString(R.string.sending), getString(R.string.sending));

			final HashMap<String, String> params = new HashMap<String, String>();
			params.put(Constant.REG_ID, regId);

			aquery.progress(dialog).ajax(url, params, JSONObject.class,
					new AjaxCallback<JSONObject>() {
						@Override
						public void callback(String url, JSONObject object,
								AjaxStatus status) {
							switch (status.getCode()) {
							case Constant.REQUEST_OK:

								int code = JsonHelper
										.getResponseCodeFromJson(object);
								System.out
										.println("SendRegId Response code is :"
												+ code);
								switch (code) {
								case Constant.REQUEST_OK:

									storeUser();
									alert = new AlertDialog.Builder(
											LoginActivity.this)
											.setTitle(R.string.information)
											.setIcon(R.drawable.successicon)
											.setMessage(
													R.string.load_successfully)
											.setPositiveButton(
													R.string.ok,
													new DialogInterface.OnClickListener() {
														@Override
														public void onClick(
																DialogInterface dialog,
																int which) {
															/*
															 * Intent intent =
															 * new Intent(
															 * LoginActivity
															 * .this,
															 * TagActivity
															 * .class);
															 * startActivity
															 * (intent);
															 */
															Intent intent = new Intent(
																	LoginActivity.this,
																	IntermediateActivity.class);
															startActivity(intent);
															finish();
														}
													});
									alert.show();
									break;

								default:
									message = getString(R.string.error_while_sending)
											+ " " + Constant.REG_ID;
									showErrorDialog(message, 1);
									break;
								}
								break;

							default:
								message = getString(R.string.error_while_sending)
										+ " " + Constant.REG_ID;
								showErrorDialog(message, 1);
								break;
							}
						}
					}.cookie(cookieName, cookieValue));
		} else {
			showInternetErrorDialog(1);
		}
	}

	public void load_categories() {
		if (InternetHelper.isOnline(LoginActivity.this)) {
			System.out
					.println("############## Loading Categories WS is called ###########");
			// String url = getString(R.string.url)+
			// getString(R.string.load_Categories);
			String url = this.getServerUrl()
					+ getString(R.string.load_Categories);
			DialogHelper.showProcessingDialog(dialog,
					getString(R.string.loading),
					getString(R.string.wait_loading));
			AjaxCallback<String> cb = new AjaxCallback<String>() {
				@Override
				public void callback(String url, String json, AjaxStatus status) {
					switch (status.getCode()) {
					case Constant.REQUEST_OK:
						if (!status.getCookies().isEmpty())
							LoginActivity.cookies = status.getCookies();
						int code = JsonHelper.getResponseCodeFromJson(json);
						switch (code) {
						case Constant.REQUEST_OK:
							Type listType = new TypeToken<ArrayList<Category>>() {
							}.getType();
							ArrayList<Category> categories = JsonHelper
									.getListObjectFromJson(json,
											Constant.LIST_CATEGORIES, listType);

							if (!categories.isEmpty())
								System.out
										.println("############## Categories is not empty ###########");
							for (Category c : categories)
								if (c.getCategoryName() != null) {
									try {
										categoryFacade.insert(c);
										System.out.println("category: "
												+ c.getCategoryName()
												+ "is charged from the server");
									} catch (Exception e) {
										e.printStackTrace();
									}
								} else {
									System.out.println("the Category number= "
											+ c.getCategoryId() + "is null");
								}
							System.out
									.println("####### End Loading Categories ######");
							load_products();
							// break;
							//
							// default:
							// message = getString(R.string.error_while_loading)
							// + " " + Constant.LIST_CATEGORIES;
							// showErrorDialog(message, 2);
							break;
						case Constant.NULL_RESPONSE:
							load_products();
							break;
						}
						break;

					default:
						message = getString(R.string.error_while_loading) + " "
								+ Constant.LIST_CATEGORIES + ". "
								+ getString(R.string.request_not_received);
						showErrorDialog(message, 2);
						break;
					}
				}
			};
			if (LoginActivity.cookies != null)
				for (Cookie cookie : LoginActivity.cookies) {
					Log.v("Adding cookie", cookie.getName() + " Value "
							+ cookie.getValue());
					cb.cookie(cookie.getName(), cookie.getValue());
				}
			aquery.progress(dialog).ajax(url, null, String.class, cb);
		} else {
			showInternetErrorDialog(2);
		}
	}

	public void load_products() {
		if (InternetHelper.isOnline(LoginActivity.this)) {
			System.out
					.println("############ Loading Products WS is called ###########");
			// String url = getString(R.string.url)+
			// getString(R.string.load_Products);
			String url = this.getServerUrl()
					+ getString(R.string.load_Products);
			DialogHelper.showProcessingDialog(dialog,
					getString(R.string.loading),
					getString(R.string.wait_loading));
			AjaxCallback<String> cb = new AjaxCallback<String>() {

				@Override
				public void callback(String url, String json, AjaxStatus status) {
					switch (status.getCode()) {
					case Constant.REQUEST_OK:
						if (!status.getCookies().isEmpty())
							LoginActivity.cookies = status.getCookies();
						int code = JsonHelper.getResponseCodeFromJson(json);
						switch (code) {
						case Constant.REQUEST_OK:
							Type listType = new TypeToken<ArrayList<Product>>() {
							}.getType();
							ArrayList<Product> products = JsonHelper
									.getListObjectFromJson(json,
											Constant.LIST_PRODUCTS, listType);
							System.out.println("products list size = "
									+ products.size());
							for (int i = 0; i < products.size(); i++) {
								System.out.println("##############  product = "
										+ products.get(i).toString());
							}

							if (!products.isEmpty())
								System.out
										.println("############ List of products is not empty ###########");
							for (Product p : products)
								try {
									if (p.getIconeURL() == null)
										p.setIconeURL("");

									if (p.getDescription() == null)
										p.setDescription("");

									productFacade.insert(p);
									System.out.println("####### product: "
											+ p.getName()
											+ " charged from the server");

									String path = getString(R.string.productPhotosOffLine)
											+ p.getIconeURL();
									File file = new File(
											Environment
													.getExternalStorageDirectory(),
											path);

									if (!file.exists()) {
										/*
										 * download(getString(R.string.url)+
										 * getString
										 * (R.string.productPhotosOnLine),
										 * p.getIconeURL(),getString(R.string.
										 * productPhotosOffLine));
										 */
										download(
												getServerUrl()
														+ getString(R.string.productPhotosOnLine),
												p.getIconeURL(),
												getString(R.string.productPhotosOffLine));
									}
								} catch (Exception e) {
									e.printStackTrace();
								}
							System.out
									.println("######## End Loading Product ########");
							// A remettre !!!
							load_aids();
							break;

						default:
							message = getString(R.string.error_while_loading)
									+ " " + Constant.LIST_PRODUCTS;
							showErrorDialog(message, 3);
							break;
						}
						break;

					default:
						message = getString(R.string.error_while_loading) + " "
								+ Constant.LIST_PRODUCTS + ". "
								+ getString(R.string.request_not_received);
						showErrorDialog(message, 3);
						break;
					}
				}
			};
			if (LoginActivity.cookies != null)
				for (Cookie cookie : LoginActivity.cookies) {
					Log.v("Adding cookie", cookie.getName() + " Value "
							+ cookie.getValue());
					cb.cookie(cookie.getName(), cookie.getValue());
				}
			aquery.progress(dialog).ajax(url, null, String.class, cb);
		} else {
			showInternetErrorDialog(3);
		}
	}

	public void load_aids() {
		if (InternetHelper.isOnline(LoginActivity.this)) {
			System.out.println("######## Loading Aids WS is called ########");
			// String url = getString(R.string.url)+
			// getString(R.string.load_Aids);
			String url = getServerUrl() + getString(R.string.load_Aids);
			DialogHelper.showProcessingDialog(dialog,
					getString(R.string.loading),
					getString(R.string.wait_loading));
			AjaxCallback<String> cb = new AjaxCallback<String>() {
				@Override
				public void callback(String url, String json, AjaxStatus status) {
					switch (status.getCode()) {
					case Constant.REQUEST_OK:
						if (!status.getCookies().isEmpty())
							LoginActivity.cookies = status.getCookies();
						int code = JsonHelper.getResponseCodeFromJson(json);
						switch (code) {
						case Constant.REQUEST_OK:
							Type listType = new TypeToken<ArrayList<Aid>>() {
							}.getType();
							ArrayList<Aid> aids = JsonHelper
									.getListObjectFromJson(json,
											Constant.LIST_AIDS, listType);

							if (!aids.isEmpty())
								System.out
										.println("######## Aids list is not empty ########");
							for (Aid a : aids)
								try {
									aidFacade.insert(a);
									System.out.println("##### aid:  "
											+ a.getAidId()
											+ "is charged from the server ");
								} catch (Exception e) {
									e.printStackTrace();
								}
							System.out
									.println("###### End Loading Aids ########");
							load_families();
							break;

						case Constant.NULL_RESPONSE:
							System.out
									.println("LoginActivity Load aids : No Aids yet");
							load_families();
							break;

						default:
							message = getString(R.string.error_while_loading)
									+ " " + Constant.LIST_AIDS;
							showErrorDialog(message, 4);
							break;
						}
						break;

					default:
						message = getString(R.string.error_while_loading) + " "
								+ Constant.LIST_AIDS + ". "
								+ getString(R.string.request_not_received);
						showErrorDialog(message, 4);
						break;
					}
				}
			};
			if (LoginActivity.cookies != null)
				for (Cookie cookie : LoginActivity.cookies) {
					Log.v("Adding cookie", cookie.getName() + " Value "
							+ cookie.getValue());
					cb.cookie(cookie.getName(), cookie.getValue());
				}
			aquery.progress(dialog).ajax(url, null, String.class, cb);
		} else {
			showInternetErrorDialog(4);
		}
	}

	public void load_families() {
		// Log.d(null, "######################" + "load families" +
		// "##################");
		if (InternetHelper.isOnline(LoginActivity.this)) {
			System.out
					.println("########## Loading Families WS is called ##########");
			String url = getServerUrl() + getString(R.string.load_Families);

			Log.d(null, "######################" + url + "##################");

			DialogHelper.showProcessingDialog(dialog,
					getString(R.string.loading),
					getString(R.string.wait_loading));
			AjaxCallback<String> cb = new AjaxCallback<String>() {

				@Override
				public void callback(String url, String json, AjaxStatus status) {
					switch (status.getCode()) {
					case Constant.REQUEST_OK:
						if (!status.getCookies().isEmpty())
							LoginActivity.cookies = status.getCookies();
						int code = JsonHelper.getResponseCodeFromJson(json);
						switch (code) {
						case Constant.REQUEST_OK:
							Type listType = new TypeToken<ArrayList<Family>>() {
							}.getType();
							ArrayList<Family> families = JsonHelper
									.getListObjectFromJson(json,
											Constant.LIST_FAMILIES, listType);

							if (!families.isEmpty())
								System.out
										.println("########## Families list is not empty ##########");
							for (Family f : families) {
								try {
									familyFacade.insert(f);
									System.out.println("##### the Family "
											+ f.getFamilyName()
											+ "is charged from the server");

									for (Beneficiary fib : f.getBeneficiaries()) {
										fib.setFamily(f);
										beneficiaryFacade.insert(fib);
										System.out
												.println("##### Beneficiary charged from server is "
														+ fib.getFirstName());
										String path = getString(R.string.benefPhotosOffLine)
												+ fib.getPhotoURL();
										File file = new File(
												Environment
														.getExternalStorageDirectory(),
												path);
										if (!file.exists()) {
											/*
											 * download( getString(R.string.url)
											 * +
											 * getString(R.string.benefPhotosOnLine
											 * ), fib.getPhotoURL(),
											 * getString(R.
											 * string.benefPhotosOffLine));
											 */
											download(
													getServerUrl()
															+ getString(R.string.benefPhotosOnLine),
													fib.getPhotoURL(),
													getString(R.string.benefPhotosOffLine));
										}
									}

									Portfolio p = f.getPortfolio();
									if (p != null) {
										System.out
												.println("##### portfolio is not empty ####");
										p.setFamily(f);
										portfolioFacade.insert(p);
										System.out
												.println("##### portfolio charged from server is: "
														+ p.getPortfolioId());

										for (PortfolioDetail pfd : p
												.getDetails()) {
											pfd.setPortfolio(p);
											portfolioDetailFacade.insert(pfd);
										}
									}

									RationCard r = f.getRationCard();
									if (r != null) {
										System.out
												.println("##### ration card is not empty ####");
										r.setFamily(f);
										rationCardFacade.insert(r);
										System.out
												.println("##### ration card charged from server is: "
														+ r.getRationCardId());
									}

									Address adr = f.getAddress();
									if (adr != null) {
										System.out
												.println("##### address is not empty ####");
										adr.setFamily(f);
										addressFacade.insert(adr);
										System.out
												.println("##### address charged from server is: "
														+ adr.getAddressId());

									}
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
							System.out
									.println("###### End Loading all Families #######");
							sendRegId();
							break;

						case Constant.NULL_RESPONSE:
							System.out
									.println("LoginActivity load families : No Families yet!");
							sendRegId();

							break;

						default:
							message = getString(R.string.error_while_loading)
									+ " " + Constant.LIST_FAMILIES;
							showErrorDialog(message, 5);
							break;
						}
						break;

					default:
						message = getString(R.string.error_while_loading) + " "
								+ Constant.LIST_FAMILIES + ". "
								+ getString(R.string.request_not_received);
						showErrorDialog(message, 5);
						break;
					}
				}
			};
			if (LoginActivity.cookies != null)
				for (Cookie cookie : LoginActivity.cookies) {
					Log.v("Adding cookie", cookie.getName() + " Value "
							+ cookie.getValue());
					cb.cookie(cookie.getName(), cookie.getValue());
				}
			aquery.progress(dialog).ajax(url, null, String.class, cb);
		} else {
			showInternetErrorDialog(5);
		}
	}

	/**
	 * Download images from external file
	 * 
	 * @param url
	 * @param imageName
	 * @param location
	 */

	public void download(String url, String imageName, String location) {
		File ext = Environment.getExternalStorageDirectory();
		File target = new File(ext, location + imageName);
		aquery.download(url + imageName, target, new AjaxCallback<File>() {
			@Override
			public void callback(String url, File file, AjaxStatus status) {
				if (file == null) {
					System.out.println("Failed to load file");
				}
			}
		});
	}

	/**
	 * Action to launch when Internet error
	 * 
	 * @param action
	 */

	public void showInternetErrorDialog(final int action) {
		AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.this);
		alert.setTitle(R.string.internet_error);
		alert.setIcon(android.R.drawable.ic_dialog_alert);
		alert.setMessage(R.string.internet_error_message);
		alert.setPositiveButton(R.string.settings,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						LoginActivity.this.startActivity(new Intent(
								Settings.ACTION_WIFI_SETTINGS));
					}
				});
		alert.setNegativeButton(R.string.retry,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						actionToDoWhenErrorOccurs(action);
					}
				});
		alert.show();
	}

	/**
	 * Action to launch when error occurred while processing with server
	 * 
	 * @param message
	 * @param action
	 */
	public void showErrorDialog(String message, final int action) {
		AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.this);
		alert.setIcon(android.R.drawable.ic_delete);
		alert.setTitle(R.string.error);
		alert.setMessage(message);
		alert.setPositiveButton(R.string.retry,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						actionToDoWhenErrorOccurs(action);
					}
				});
		alert.setNegativeButton(R.string.close,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		alert.show();
	}

	@Override
	public void onAPDUError(APDUError error) {
		Toast.makeText(this, error.getMessage(), Toast.LENGTH_LONG).show();
		System.out.println("error code " + error.getApduError()[0]);
	}

	/**
	 * on server response success
	 */
	@Override
	public void onResponse(Map<String, Object> results, int commandId) {
		switch (commandId) {
		case Constant.GET_LOGIN_INFO_CODE:
			if (results.size() >= 2) {
				usernameStored = String.valueOf(results.get("username"));
				passwordStored = String.valueOf(results.get("password"));
				checkLoginInfo(username, password);
			}
			break;
		case Constant.SET_LOGIN_INFO_CODE:
			hasRun = true;
			SharedPreferences.Editor edit = settings.edit();
			edit.putBoolean("hasRun", true); // set to has run
			edit.commit(); // apply
			break;
		}
	}

	private FIAgent retrieveUserStored() {
		String firstNameStored = settings.getString("firstName", "");
		String lastNameStored = settings.getString("lastName", "");
		String sessionIdStored = settings.getString("sessionId", "");
		int agentIdStored = settings.getInt("agentId", 0);
		return new FIAgent(agentIdStored, firstNameStored, lastNameStored,
				sessionIdStored);
	}

	/**
	 * verifying the entered data is the same as the stored data
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	private boolean loginAndPasswordOk(String username, String password) {
		boolean ok = (username.equals(usernameStored) && password
				.equals(passwordStored));
		return ok;

	}

	private void storeUser() {
		SharedPreferences.Editor edit = settings.edit();
		// Storing the user agent data
		edit.putInt("agentId", user.getId());
		edit.putString("firstName", user.getFirstName());
		edit.putString("lastName", user.getLastName());
		edit.putString("sessionId", user.getSessionId());
		edit.commit(); // apply
	}

	public void actionToDoWhenErrorOccurs(int action) {
		switch (action) {
		case 0:
			if (InternetHelper.isOnline(LoginActivity.this)) {
				/*
				 * async_post(getString(R.string.url) +
				 * getString(R.string.connexion), true, false, usernameStored,
				 * passwordStored);
				 */
				async_post(getServerUrl() + getString(R.string.connexion),
						true, false, usernameStored, passwordStored);
			} else {
				showInternetErrorDialog(0);
			}
			break;
		case 1:
			sendRegId();
			break;
		case 2:
			load_categories();
			break;
		case 3:
			load_products();
			break;
		case 4:
			load_aids();
			break;
		case 5:
			load_families();
			break;
		}
	}

}
